require_relative 'npi'
require_relative 'pdu'
require_relative 'ton'

module SmppClient

  module Pdu

    # An ESME bound as a Receiver is authorised to receive short messages from the SMSC and to
    # return the corresponding SMPP message responses to the SMSC.

    class BindReceiver < Pdu

      attr_accessor :system_id, :password, :system_type, :interface_version,
                  :addr_ton, :addr_npi, :address_range

      def initialize(byte_buffer = nil)
        if byte_buffer == nil
          super(CommandId::BIND_RECEIVER)
          @system_id = ''
          @password = ''
          @system_type = ''
          @interface_version = 0
          @addr_ton = Ton::UNKNOWN
          @addr_npi = Npi::UNKNOWN
          @address_range = ''
        else
          super(byte_buffer)
          parse_body(byte_buffer)
          parse_tlvs(byte_buffer)
        end
      end

      def parse_body(byte_buffer)
        @system_id = byte_buffer.remove_cstring
        @password = byte_buffer.remove_cstring
        @system_type = byte_buffer.remove_cstring
        @interface_version = byte_buffer.remove_byte
        @addr_ton = byte_buffer.remove_byte
        @addr_npi = byte_buffer.remove_byte
        @address_range = byte_buffer.remove_cstring
      end

      def body_bytes
        buffer = ByteBuffer.new
        buffer.append_cstring(@system_id)
        buffer.append_cstring(@password)
        buffer.append_cstring(@system_type)
        buffer.append_byte(@interface_version)
        buffer.append_byte(@addr_ton)
        buffer.append_byte(@addr_npi)
        buffer.append_cstring(@address_range)
        return buffer.bytes
      end

    end

  end

end